package com.app.onebuy.basis;

import android.app.Notification;
import android.content.Context;
import android.os.Handler;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.widget.ImageView;
import android.widget.Toast;

import com.app.onebuy.R;
import com.bumptech.glide.Glide;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.BounceEnter.BounceBottomEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatformConfig;
import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import my.http.MyInterceptor;
import okhttp3.OkHttpClient;

public class BasisApp extends MultiDexApplication {
    private static BasisApp APP;

    public static Context mContext;
    private static Handler handler;
    public static String DIR_PATH_USER;

    public static boolean isProgramExit = false;
    static Toast mToast;

    public static BaseAnimatorSet bas_in = new BounceBottomEnter();
    public static BaseAnimatorSet bas_out = new SlideBottomExit();

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getBaseContext();
        isProgramExit = false;
        APP = this;
//		SDKInitializer.initialize(this);
        handler = new Handler();
        DIR_PATH_USER = getFilesDir().getAbsolutePath() + "/";
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
//				SDKInitializer.initialize(this);
//		Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
        ChangeLanguageHelper.init(this);


        initJpush();
        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
        initOkHttp();
    }

    private void initOkHttp(){

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new MyInterceptor())
                //其他配置
                .build();
//        OkHttpUtils.initClient(okHttpClient);
    }
    private void initJpush() {
        JPushInterface.init(this);            // 初始化 JPush
        JPushInterface.setDebugMode(Constants.DEBUG);    // 设置开启日志,发布时请关闭日志
        BasicPushNotificationBuilder builderB = new BasicPushNotificationBuilder(this);
//        builderB.statusBarDrawable = R.drawable.icon_notification_small;
        builderB.notificationFlags = Notification.FLAG_AUTO_CANCEL
                | Notification.FLAG_SHOW_LIGHTS;  //设置为自动消失和呼吸灯闪烁
        builderB.notificationDefaults = Notification.DEFAULT_SOUND
                | Notification.DEFAULT_VIBRATE
                | Notification.DEFAULT_LIGHTS;  // 设置为铃声、震动、呼吸灯闪烁都要
        JPushInterface.setPushNotificationBuilder(1, builderB);

//        CustomPushNotificationBuilder builder = new
//                CustomPushNotificationBuilder(this,
//                R.layout.customer_notitfication_layout,
//                R.id.icon,
//                R.id.title,
//                R.id.text);
//        // 指定定制的 Notification Layout
//        builder.statusBarDrawable = R.drawable.icon_notification_small;
//        // 指定最顶层状态栏小图标
//        builder.layoutIconDrawable = R.drawable.icon_notification_small;
//        // 指定下拉状态栏时显示的通知图标
//        JPushInterface.setPushNotificationBuilder(2, builder);

        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

        JShareInterface.setDebugMode(true);
        PlatformConfig platformConfig = new PlatformConfig()
//				.setWechat("wxc40e16f3ba6ebabc", "dcad950cd0633a27e353477c4ec12e7a")
//				.setQQ("1106011004", "YIbPvONmBQBZUGaN")
//				.setSinaWeibo("374535501", "baccd12c166f1df96736b51ffbf600a2", "https://www.jiguang.cn")
                .setFacebook("606374193153039", "1mwinner")
                .setTwitter("GkOvtHJxXKTT4IuiZrwJnq3Ly", "Nwo33g2hJsiFJkVY8X2HGkxzDJpB2EuwC3mBJQGj1oGA03acFC");
        JShareInterface.init(this, platformConfig);


        //极光统计
        JAnalyticsInterface.init(this);
        JAnalyticsInterface.setDebugMode(Constants.DEBUG);
        JAnalyticsInterface.initCrashHandler(this);
    }


    public static BasisApp getInstance() {
        return APP;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }


    /**
     * 短消息提示
     */
    public static void showToast(final int resId) {
        handler.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
//				if (mToast != null) {
//					mToast.cancel();
//				}
                mToast = Toast.makeText(mContext, resId, Toast.LENGTH_SHORT);
                mToast.show();
            }
        });
    }

    /**
     * 短消息提示
     */
    public static void showToast(final String text) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
//				if (mToast != null) {
//					mToast.cancel();
//				}
                mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
                mToast.show();
            }
        });
    }


    public static void loadImg(Context mContext, String imgurl, ImageView imageView, int res_default) {

//		if(TextUtil.isEmpty(imgurl))return;
        Glide.with(mContext).load(imgurl)
//				.apply(new RequestOptions().placeholder(res_default).error(res_default))
                .placeholder(res_default).error(res_default)
                .into(imageView);
    }

    public static void loadImg(Context mContext, String imgurl, ImageView imageView) {
        loadImg(mContext, imgurl, imageView, R.drawable.base_transparent);
    }

    public static void loadImg(String imgurl, ImageView imageView) {
        loadImg(BasisApp.mContext, imgurl, imageView, R.drawable.base_transparent);
    }

    public static void loadImgAsbitmap(Context mContext, String imgurl, ImageView imageView, int res_default) {

//		if(TextUtil.isEmpty(imgurl))return;
        Glide.with(mContext)
//				.asBitmap()
                .load(imgurl)
                .asBitmap()
                .placeholder(res_default)
//				.apply(new RequestOptions().placeholder(res_default).error(res_default))
                .into(imageView);
    }

    public static String getStringL(int resid) {
        return mContext.getResources().getString(resid);
//		return  ChangeLanguageHelper.getStringById(resid);
    }
}
